<?php

namespace app\api\controller\Project;

use think\Controller;
use think\Request;

class Project extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->UserModel = model('User.User');
        $this->AddressModel = model('User.Address');
        $this->ProjectOrderModel = model('Project.Order');
        $this->RecordModel = model('User.Record');
        $this->ProjectModel = model('Project.Project');
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);

            $status = $this->request->param('status','');

            $where = [
                'userid' => $userid,
            ];

            if($status != '')
            {
                $where['status'] = $status;
            }

            
            $ProList = $this->ProjectOrderModel->with(['project'])->where($where)->order('id','desc')->select();

            $this->success('返回预约数据',null,$ProList);
            exit;
        }
    }

    public function type()
    {
        if($this->request->isPost())
        {
            $ProList = $this->ProjectModel->order('id','desc')->select();

            $this->success('返回预服务数据成功',null,$ProList);
            exit;
        }
    }

    public function add()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid',0);
            $addrid = $this->request->param('addrid',0);
            $projectid = $this->request->param('projectid',0);
            $content = $this->request->param('content','');
            $createtime = $this->request->param('createtime',time());

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在，请登录');
                exit;
            }

            $address = $this->AddressModel->find($addrid);

            if(!$address)
            {
                $this->error('所选的上门地址不存在');
                exit;
            }

            $project = $this->ProjectModel->find($projectid);

            if(!$project)
            {
                $this->error('所选的服务不存在');
                exit;
            }

            // 毫秒换成秒
            $createtime = $createtime / 1000;

            // 现在的时间
            $now = time() + 3600;

            if($createtime < $now)
            {
                $this->error('预约时间至少提前一个小时');
                exit;
            }

            $data = [
                'userid' => $userid,
                'addrid' => $addrid,
                'projectid' => $projectid,
                'content' => $content,
                'status' => 0,
                'createtime' => $createtime
            ];

            $result = $this->ProjectOrderModel->save($data);

            if($result === FALSE)
            {
                $this->error('预约失败');
                exit;
            }else{
                $this->success('预约成功');
                exit;
            }
        }
    }

    public function info()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid');

            $info = $this->ProjectOrderModel->find($proid);

            if(!$info)
            {
                $this->error('该预约记录不存在');
                exit;
            }

            $this->success('返回预约详情数据',null,$info);
            exit;
        }
    }
    public function pay()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid',0);
            $userid = $this->request->param('userid',0);

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $info = $this->ProjectOrderModel->with(['project'])->find($proid);

            if(!$info)
            {
                $this->error('该预约不存在');
                exit;
            }

            $this->UserModel->startTrans();
            $this->RecordModel->startTrans();
            $this->ProjectOrderModel->startTrans();

            // 更新售后订单状态
            $OrderData = [
                'id' => $proid,
                'status' => 2
            ];

            $OrderStatus = $this->ProjectOrderModel->isUpdate(true)->save($OrderData);

            if($OrderStatus === FALSE)
            {
                $this->error('售后订单状态更新失败');
                exit;
            }

            $point = bcsub($user['point'],$info['price']);

            if($point < 0)
            {
                $this->error('积分不足，请充值');
                exit;
            }

            $UserData = [
                'id' => $userid,
                'point' => $point
            ];

            $UserStatus = $this->UserModel->isUpdate(true)->save($UserData);

            if($UserStatus === FALSE)
            {
                $this->ProjectOrderModel->rollback();
                $this->error('用户积分更新失败');
                exit;
            }

            $RecordData = [
                'userid' => $userid,
                'point' => $point,
                'content' => "你的售后项目{$info['project']['name']},内容为{$info['content']},消费了{$info['price']}积分",
                'state' => 6
            ];

            $RecordStatus = $this->RecordModel->validate('common/User/Record')->save($RecordData);

            if($RecordStatus === FALSE)
            {
                $this->ProjectOrderModel->rollback();
                $this->UserModel->rollback();
                $this->error($this->RecordModel->getError());
                exit;
            }

            if($OrderStatus === FALSE || $UserStatus === FALSE || $RecordStatus === FALSE )
            {
                $this->ProjectOrderModel->rollback();
                $this->RecordModel->rollback();
                $this->UserModel->rollback();
                $this->error('下单失败，请重新下单');
            }else{
                $this->ProjectOrderModel->commit();
                $this->RecordModel->commit();
                $this->UserModel->commit();
                $this->success('下单成功');
                exit;
            }


        }
    }
}